#coding:utf8
# vim: tabstop=4 shiftwidth=4 softtabstop=4
"""
Created on May 8, 2013

@author: join
"""
from rest_framework import status
from cvirt.manager.serializers import GraphSerializer
from rest_framework.response import Response
from rest_framework.generics import GenericAPIView
from cvirt.manager.monitor.graph import Grapher
from cvirt.common import translate as _

class HostMonitorGraphs(GenericAPIView):
    serializer_class = GraphSerializer
    #     permission_classes = (permissions.IsAuthenticated,)
    #     authentication_classes = (ExpiringTokenAuthentication,)

    def post(self,request,*args,**kwargs):
        try:
            serializer = GraphSerializer(data=request.DATA)
            params = request.DATA
            if serializer.is_valid():
                hostname = params.get('hostname',None)
                metric = params.get('metric',None)
                starttime = params.get('starttime',None)
                endtime = params.get('endtime',None)
                timerange = params.get('timerange',None)
                type = params.get('type')

                if not timerange:
                    grapher = Grapher(hostname,metric,starttime,endtime,timerange,type)
                else:
                    grapher = Grapher(hostname,metric,None,None,timerange,type)
                result = grapher.graph()
                return Response(result)
            else:
                return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        except:
            return Response(_("获取监控数据失败"), status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        
    def get(self,request,*args,**kwargs):
        serializer = GraphSerializer(data=request.DATA)
        return Response(request.DATA)
    